package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.xmy.cultivate.entity.EnrollSchoolMember;
import com.xmy.cultivate.service.IEnrollSchoolMemberService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.List;
import java.util.Map;

//校区管理->添加教师
@RestController
@RequestMapping("/api/enrollSchoolMember")
@Scope("prototype")
public class EnrollSchoolMemberController {
    @Autowired
    private IEnrollSchoolMemberService enrollSchoolMemberService;

    @GetMapping()
    public Result enrollSchoolMemberList(@RequestParam Map<String, Object> reMap){
        QueryWrapper<EnrollSchoolMember> enrollSchoolMemberQueryWrapper = new QueryWrapper<>();
        enrollSchoolMemberQueryWrapper.select("*,(SELECT name FROM staff WHERE id = enroll_school_member.teacher_id LIMIT 1) AS teacherName");
        if (reMap.get("enrollSchoolId") != null && !reMap.get("enrollSchoolId").toString().isEmpty()) {
            enrollSchoolMemberQueryWrapper.eq("enroll_school_id", reMap.get("enrollSchoolId"));
        }
        enrollSchoolMemberQueryWrapper.eq("status", 1);
        enrollSchoolMemberQueryWrapper.orderByDesc("id","status");
        List<EnrollSchoolMember> list = enrollSchoolMemberService.list(enrollSchoolMemberQueryWrapper);
        return new Result(Code.OK, list);

    }

    @PostMapping()
    public Result addEnrollSchoolMemberGet(@RequestBody EnrollSchoolMember enrollSchoolMember){
        if (enrollSchoolMember.getTeacherIdList() != null && !enrollSchoolMember.getTeacherIdList().isEmpty()) {
            for (Long id: enrollSchoolMember.getTeacherIdList()) {
                EnrollSchoolMember enrollSchoolMemberN = new EnrollSchoolMember();
                enrollSchoolMemberN.setEnrollSchoolId(enrollSchoolMember.getId());
                enrollSchoolMemberN.setTeacherId(id);
                enrollSchoolMemberN.setStartDate(LocalDate.now());
                addEnrollSchoolMember(enrollSchoolMemberN);
            }
        }
        return new Result(Code.OK);
    }

    public void addEnrollSchoolMember(EnrollSchoolMember enrollSchoolMember){
        long count = enrollSchoolMemberService.count(new QueryWrapper<EnrollSchoolMember>().eq("teacher_id", enrollSchoolMember.getTeacherId()).eq("enroll_school_id", enrollSchoolMember.getEnrollSchoolId()).eq("status",1));
        if (count == 0L) {
            enrollSchoolMemberService.save(enrollSchoolMember);
        }
    }

    //切换
    @PutMapping()
    public Result updateEnrollSchoolMember(@RequestBody EnrollSchoolMember enrollSchoolMember){
        //当前要切换的id
        EnrollSchoolMember byId = enrollSchoolMemberService.getOne(new QueryWrapper<EnrollSchoolMember>().eq("id", enrollSchoolMember.getId()).eq("status", 1));
        if (enrollSchoolMember.getEnrollSchoolId().equals(byId.getEnrollSchoolId())) {
            return new Result(Code.ERROR,"","不能换到相同校区！");
        }

        if (byId != null) {
            if (byId.getStartDate().isBefore(enrollSchoolMember.getEndDate())) {
                return new Result(Code.ERROR, "", "截止日期，不能小于转出校区的开始时间！");
            }
            String atSchoolStr = enrollSchoolMemberService.getAtCount(byId.getId(), byId.getTeacherId(), enrollSchoolMember.getEndDate());
            if (StringUtils.isNotBlank(atSchoolStr)) {
                return new Result(Code.ERROR, "", "当前时间与已存在的“" + atSchoolStr + "”冲突，不能切换，请选择其它时间");
            }
            if (byId.getStatus().equals(0)){
                return new Result(Code.ERROR, "", "当前状态不能切换！");
            }


            //不是相同校区，移出战队并往下新添加一条数据
            byId.setStatus(0);//原先的改成0表示移出战队，1目前在战队
            byId.setEndDate(enrollSchoolMember.getEndDate());
            enrollSchoolMemberService.updateById(byId);

            //新添加一条数据到切换的目标校区
            EnrollSchoolMember enrollSchoolMemberAdd = new EnrollSchoolMember();
            enrollSchoolMemberAdd.setEnrollSchoolId(enrollSchoolMember.getEnrollSchoolId());// 切换的校区id
            if (enrollSchoolMember.getEndDate() != null) {
                enrollSchoolMemberAdd.setStartDate(byId.getEndDate().plusDays(1));// 开始日期，在当前日期基础上加一天
                //enrollSchoolMemberAdd.setEndDate(enrollSchoolMember.getEndDate());// 结束日期
            }
            enrollSchoolMemberAdd.setTeacherId(byId.getTeacherId());// 教师id
            enrollSchoolMemberAdd.setStatus(1);//0表示移出战队，1目前在战队
            enrollSchoolMemberAdd.setEndDate(null);
            enrollSchoolMemberService.save(enrollSchoolMemberAdd);
            return new Result(Code.OK, "", "切换成功！");
        }else {
            return new Result(Code.OK, "", "校区不存在！");
        }
    }

    //校区约束删除
    @DeleteMapping()
    public Result deleteEnrollSchoolMemberUpdate(@RequestBody EnrollSchoolMember enrollSchoolMember){
        if (enrollSchoolMember.getTeacherIdList() != null && !enrollSchoolMember.getTeacherIdList().isEmpty()) {
            for (Long id: enrollSchoolMember.getTeacherIdList()) {
                EnrollSchoolMember one = enrollSchoolMemberService.getOne(new QueryWrapper<EnrollSchoolMember>().eq("teacher_id", id).eq("enroll_school_id", enrollSchoolMember.getId()).eq("status",1).last("limit 1"));
                if (one != null) {
                    one.setStatus(0);
                    enrollSchoolMemberService.updateById(one);
                }
            }
        }
        return new Result(Code.OK);
    }

    //添加错误删除
    @DeleteMapping("delBatchMember")
    public Result deleteEnrollSchoolMemberRemove(@RequestBody EnrollSchoolMember enrollSchoolMember){
        if (enrollSchoolMember.getTeacherIdList() != null && !enrollSchoolMember.getTeacherIdList().isEmpty()) {
            for (Long id: enrollSchoolMember.getTeacherIdList()) {
                EnrollSchoolMember one = enrollSchoolMemberService.getOne(new QueryWrapper<EnrollSchoolMember>().eq("teacher_id", id).eq("enroll_school_id", enrollSchoolMember.getId()).eq("status",1).last("limit 1"));
                if (one != null) {
                    enrollSchoolMemberService.removeById(one.getId());
                }
            }
        }
        return new Result(Code.OK);
    }

}
